package comgame.scenes;

import com.almasb.fxgl.app.scene.FXGLScene;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.audio.Music;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.geometry.Insets;

import java.util.Objects;

public class EndScene extends FXGLScene {
    private StackPane sceneRoot;
    private static Music endMusic;
    public EndScene() {
        sceneRoot = new StackPane();
        sceneRoot.setPrefSize(800, 600);
        sceneRoot.setStyle("-fx-background-color: black;");

        // 加载END图片
        Image endImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/END.jpg")));
        ImageView endView = new ImageView(endImg);
        endView.setPreserveRatio(true);
        endView.setFitWidth(500);

        // 返回主菜单按钮
        Button backBtn = new Button("返回主菜单");
        backBtn.setFont(Font.font("Arial", 22));
        backBtn.setStyle("-fx-background-color: #FFD700; -fx-text-fill: #222; -fx-background-radius: 10; -fx-padding: 10 30;");
        backBtn.setOnAction(e -> {
            FXGL.getGameScene().clearUINodes();
            MenuScene menuScene = new MenuScene();
            FXGL.getGameScene().addUINode(menuScene.getRoot());
            stopEndMusic();
        });

        VBox vbox = new VBox(40, endView, backBtn);
        vbox.setAlignment(Pos.CENTER);
        vbox.setPadding(new Insets(60, 0, 0, 0));
        sceneRoot.getChildren().add(vbox);
        StackPane.setAlignment(vbox, Pos.CENTER);

        playEndMusic();
    }

    private void playEndMusic() {
        MusicManager.stopAll();
        if (endMusic == null) {
            endMusic = FXGL.getAssetLoader().loadMusic("END.mp4");
        }
        FXGL.getAudioPlayer().loopMusic(endMusic);
    }

    private void stopEndMusic() {
        if (endMusic != null) {
            FXGL.getAudioPlayer().stopMusic(endMusic);
        }
    }

    public StackPane getSceneRoot() {
        return sceneRoot;
    }
} 